package com.dragon.concurrent.demo.waitnotify;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/1 21:21
 */
public class SimpleWN {
    final static Object object = new Object();

    public static class T1 extends Thread {
        T2 t2;

        public void setThread(T2 t2) {
            this.t2 = t2;
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + "T1 start");
                System.out.println(System.currentTimeMillis() + "T1 wait for object");
                System.out.println("T2的状态:" + t2.getState());
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    System.out.println("t1线程被打断");
                }
                System.out.println(System.currentTimeMillis() + "T1 end");
            }
        }
    }

    public static class T2 extends Thread {
        T1 t1;

        public void setThread(T1 t1) {
            this.t1 = t1;
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + "T2 start! notify one thread");
                System.out.println(System.currentTimeMillis() + "T2 end");
                System.out.println("T1的状态:" + t1.getState());
                object.notify();
                try {
                    Thread.sleep(2000);

                } catch (InterruptedException e) {
                    e.printStackTrace();

                }

            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T1 t1 = new T1();
        T2 t2 = new T2();
        t1.setThread(t2);
        t2.setThread(t1);
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t1.start();
        t2.start();
    }
}
